<template>
  <div class="data-table-container">
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="discountName" label="活动名称" min-width="120" />
      <el-table-column prop="content" label="活动内容" min-width="150" />
      <el-table-column prop="state" label="活动状态" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.state === 0">报名未开始</el-tag>
          <el-tag type="warning" v-else-if="scope.row.state === 1">报名进行中</el-tag>
          <el-tag type="info" v-else-if="scope.row.state === 2">活动待开始</el-tag>
          <el-tag type="success" v-else-if="scope.row.state === 3">活动进行中</el-tag>
          <el-tag type="danger" v-else-if="scope.row.state === 4">活动已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报名时间" width="180">
        <template #default="scope">
          {{ scope.row.signTime }}
        </template>
      </el-table-column>
      <el-table-column label="起止时间" width="180">
        <template #default="scope"> {{ scope.row.time }}<br /> </template>
      </el-table-column>
      <el-table-column prop="shops" label="商家数" width="80" />
      <el-table-column prop="products" label="商品数量" width="100" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">详情</el-button>
          <el-button
            v-if="scope.row.state === 4"
            type="danger"
            link
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue'

  // 定义props
  const props = defineProps({
    tableData: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    total: {
      type: Number,
      default: 0
    }
  })

  // 定义emits
  const emit = defineEmits(['view', 'edit', 'end', 'delete', 'size-change', 'current-change'])

  // 分页相关
  const currentPage = ref(1)
  const pageSize = ref(10)

  // 处理查看事件
  const handleView = (row: any) => {
    emit('view', row)
  }

  // 处理编辑事件
  const handleEdit = (row: any) => {
    emit('edit', row)
  }

  // 处理结束活动事件
  const handleEnd = (row: any) => {
    emit('end', row)
  }

  // 处理删除事件
  const handleDelete = (row: any) => {
    emit('delete', row)
  }

  // 处理分页大小变化
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    emit('size-change', val)
  }

  // 处理当前页变化
  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    emit('current-change', val)
  }

  // 监听total变化，重置当前页
  watch(
    () => props.total,
    (newVal, oldVal) => {
      if (newVal !== oldVal) {
        currentPage.value = 1
      }
    }
  )
</script>

<style scoped lang="scss">
  .data-table-container {
    .pagination-container {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
      overflow-x: auto;
      white-space: nowrap;
      padding-bottom: 10px;
    }
    
    /* 隐藏滚动条但保留滚动功能 */
    .pagination-container::-webkit-scrollbar {
      display: none;
    }
    .pagination-container {
      -ms-overflow-style: none;
    }
  }
  
  /* 移动端响应式调整 */
  @media screen and (max-width: 768px) {
    .data-table-container {
      .pagination-container {
        justify-content: flex-start;
        padding-left: 10px;
        padding-right: 10px;
      }
    }
  }
</style>
